package j.s.code.jsnippets.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import j.s.code.jsnippets.controller.bean.SerialResult;
import j.s.code.jsnippets.serial.SerialWorkRun;

/**
 * SerialWorkRunController
 */
@RestController
@RequestMapping("/serial")
public class SerialWorkRunController {
    @RequestMapping(method=RequestMethod.POST,value="/getSerial")
    public SerialResult getSerial(Long workNo, Long machineNo ) {
        SerialResult model = new SerialResult();
        try {
            SerialWorkRun SerialWorkRun = new SerialWorkRun();
            long id = SerialWorkRun.getSerial(workNo, machineNo);
            model.setCode("0000");
            model.setMsg("SUCCESS");
            model.setSerial(id);
            return model;
        } catch (Exception e) {
            String msg = e.getMessage();
            if(e instanceof NullPointerException){
                msg="参数为空";
            }
            model.setCode("9999");
            model.setMsg(msg);
            return model;
        }
    }
    /**
     * 缓存事先生成好的serial
     * @param SerialWorkRun
     */
    public void start_(SerialWorkRun SerialWorkRun) throws Exception {
        for (int i = 0; i < 100; i++) {
            System.out.println("--------------------start---------------------------");
            long id = SerialWorkRun.getSerial(31L, 23L);
            System.out.println(String.format("线程%s->获取id：%d", Thread.currentThread().getName(), id));
            String id_ = SerialWorkRun.parseId2Str(id);
            System.out.println(String.format("解析id：%s", id_));
            System.out.println("--------------------end---------------------------");
        }
    }
}